package dao;

import common.StuMidClass;
import service.FlushCourse;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AddStu {
    private static Connection conn=DataBaseConnect.getConn();
    private static String sql="select id from account where name=? and number=?";
    private static PreparedStatement pstmt;
    static {
        try {
            pstmt=conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static boolean addStu(StuMidClass stuMidClass){
        try {
            pstmt.setString(1,stuMidClass.getName());
            pstmt.setString(2,stuMidClass.getNumber());
            ResultSet rs=pstmt.executeQuery();
            while(rs.next()){
                String studentid=rs.getString(1);
                if(AddClass.addClass(studentid,stuMidClass.getCourseid())){
                    System.out.println("添加学生成功");
                    FlushCourse.flush(studentid);
                    return true;
                }
            }
            System.out.println("添加学生失败");
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("添加学生出现异常");
            return false;
        }
    }
}
